let ueRender;
window.onload = function() {
  //1.请求负载均衡服务获取推流地址
  //2.
  ueRender = new UERender({
    elementId: "player",
    serverHost: "192.168.50.90:8888", //推流服务地址
    ue4name: "test", //ue4名称
    print_inputs: false, //控制台日志开关
    pick_mouse: 0, //pick操作的触发鼠标键，0-左键，1-中建 2-右键
    onLoadStateChange: function(s) {
      //加载状态
      if (s === LoadStateType.Waiting) {
        document.getElementById("msgDiv").innerHTML = "正在请求渲染资源...";
      } else if (s === LoadStateType.Connecting) {
        document.getElementById("msgDiv").innerHTML =
          "连接渲染服务成功，正在加载场景...";
      } else if (s === LoadStateType.Connected) {
        document.getElementById("msgDiv").innerHTML =
          "场景加载成功，即将进入渲染世界...";
      }
    },
    onLoadComplete: function() {
      //加载完成后隐藏loading
      document.getElementById("loading").style.display = "none";

      // //忽略对地板的拾取
      // pickignore();

      // ueRender.call(FUNC_CODE.Object.get, 'S_SYSM_001', function (e) {
      //     console.log('上游水位', 'S_SYSM_001：', e);
      // });

      ueRender.call(FUNC_CODE.Object.move, {
        id: "S_SYSM_001",
        vector: { z: 1 },
        duration: 1
      });
    },
    //添加标签点击事件
    onLabelClick: function(e) {
      // e.id 标签ID
      // e.type  0:点击标签本地（默认） 1:点击标签某个部分，需要UE4处理
      console.log("标签点击：", e);
      if (e.id === "mark3") {
        let p = {
          type: "monitor",
          scale: 1,
          offset: { x: 30, y: 0 },
          config: [
            {
              id: "monitor1",
              clickable: true,
              objectid: "S_ZSMC_24",
              title: "2#监测点",
              state: "警告",
              time: "2022-11-11 14:17:39",
              pressure: "99.99",
              temp: "36.85℃"
            }
          ]
        };
        ueRender.call(FUNC_CODE.ScreenLable.AddOrUpdate, p);
      } else if (e.id === "monitor1" && e.type === 1) {
        console.log("查看监测历史");
      }
    }
  });
  //开启推流
  ueRender.startPlay();
};

function autoRotate() {
  let param = {
    speed: 5, //旋转速度，度/秒
    pitch: -30, //俯仰角
    center: { x: 0, y: 0, z: 1000 } //旋转中心点
  };
  ueRender.call(FUNC_CODE.Camera.autoRotate, param);
}

let preInfo;

function cameraInfo() {
  ueRender.call(FUNC_CODE.Camera.getInfo, undefined, function(e) {
    console.log("info:", e);
    preInfo = e;
  });
}

function cameraLock() {
  ueRender.call(FUNC_CODE.Camera.lock, {
    move: false,
    rotate: false
  });
}

function cameraRange() {
  ueRender.call(FUNC_CODE.Camera.setRange, {
    location: [
      { x: 10000, y: 10000, z: 10000 },
      { x: -10000, y: -10000, z: -100 }
    ],
    pitch: [-90, 90]
  });
}

function flyto() {
  let param = preInfo;
  if (!param) {
    param = {
      location: {
        x: -10516.2685546875,
        y: 1482.43115234375,
        z: 5288.52880859375
      },
      pitch: -7.029561996459961,
      yaw: 4.775756359100342
    };
  }
  param.duration = 1;
  //param.speed = 500;
  ueRender.call(FUNC_CODE.Camera.flyto, param);
}

let pickId;
function PickSwitch() {
  ueRender.pickable = !ueRender.pickable;
  if (ueRender.pickable) {
    console.log("拾取开");
    ueRender.onPick = function(e) {
      console.log("pick:", JSON.stringify(e));
      if (pickId) {
        //取消高亮
        //ueRender.call(FUNC_CODE.Object.highlight,{id:pickId,isHighlight:false});
      }
      //实现点击物体高亮
      if (e["object"]) {
        let p = {
          id: e["object"].id,
          isHighlight: true,
          style: { borderColor: "#ff00ffff" }
        };
        ueRender.call(FUNC_CODE.Object.highlight, p);
        pickId = p.id;
      }
    };
  } else {
    console.log("拾取关");
  }
}

function cameraFocus() {
  ueRender.call(FUNC_CODE.Camera.focus, {
    id: pickId,
    yaw: 350,
    pitch: -12,
    distance: 800
  });
}

function custom() {
  ueRender.custom("code1", { msg: "test custom" });
  ueRender.custom("code2", {}, function(e) {
    console.log("自定义接口返回：", e);
  });
}

//忽略对地板的点击拾取
function pickignore() {
  // ueRender.call(FUNC_CODE.Object.addPickIgnore,{ids:['Floor']});

  var ids = [];
  //土地
  for (let index = 1; index <= 5; index++) {
    const element = index;
    ids.push("S_DX_0" + index);
  }
  // //水体
  // ids.push("S_SYSM_001");
  // ids.push("S_XYSM_001");
  // ids.push("S_XYSM_002");
  // //流体
  // for (let index = 1; index <= 26; index++) {
  //     ids.push("BP_FluxModifierSourceActor" + index);
  // }
  // ids.push("BPFluxSimulation");
  // ids.push("EUA_FluxSimulationEditor");
  // ids.push("BP_FluxSurface_Ocean");
  // ids.push("BP_FluxSurface_Ocean_2");
  // ids.push("BP_FluxSimulation_5");

  ueRender.call(FUNC_CODE.Object.addPickIgnore, { ids: ids });
}

function highlightAll() {
  ueRender.call(FUNC_CODE.Object.highlight, { id: "*" });
}

let isHighlight = true;
function highlight() {
  let param = {
    id: "Cube", //对象ID
    isHighlight: isHighlight, //是否高亮
    style: {
      borderColor: "#ffcc00ff", //边框颜色RGBA，A表示透明度
      borderThickness: 3, //边框厚度
      fillColor: "#00000000" //填充颜色RGBA，无填充则透明度A为0
    }
  };
  //ueRender.call(FUNC_CODE.Object.highlight,param);
  param.id = "SM_Houseboat01";
  param.style.fillColor = "0xcc33ffff";
  ueRender.call(FUNC_CODE.Object.highlight, param);
  isHighlight = !isHighlight;
}

function objectmove() {
  ueRender.call(FUNC_CODE.Object.move, {
    id: "SM_Houseboat01",
    vector: { x: 0, y: 5000, z: 0 },
    duration: 1
  });
}

function objectrotate() {
  ueRender.call(FUNC_CODE.Object.rotate, {
    id: "Actor_XSZ_24",
    speed: 10,
    vector: { yaw: 0, pitch: 0, roll: 30 }
  });
}

function objectrotate2() {
  ueRender.call(FUNC_CODE.Object.rotate, {
    id: "Actor_XSZ_24",
    speed: 10,
    vector: { yaw: 0, pitch: 0, roll: -30 }
  });
}

function objectrotate3() {
  ueRender.call(FUNC_CODE.Object.rotate, {
    id: "S_XZSRZM-4",
    speed: 10,
    vector: { yaw: 75, pitch: 0, roll: 0 }
  });
  ueRender.call(FUNC_CODE.Object.rotate, {
    id: "S_XZSRZM-3",
    speed: 10,
    vector: { yaw: -75, pitch: 0, roll: 0 }
  });
}

function objectscale() {
  ueRender.call(FUNC_CODE.Object.scale, {
    id: "Cube2",
    vector: { x: 1, y: 1, z: 1 }
  });
}

let hide = false;
function showHide() {
  if (hide) {
    ueRender.call(FUNC_CODE.Object.show, { ids: ["Cube", "Cube2"] });
  } else {
    ueRender.call(FUNC_CODE.Object.hide, { ids: ["Cube", "Cube2"] });
  }
  hide = !hide;
}

function setmat() {
  ueRender.call(FUNC_CODE.Material.set, {
    id: pickId,
    mats: [{ index: 0, path: "/Game/test/MyMat" }]
  });
}

function getmat() {
  ueRender.call(FUNC_CODE.Material.get, { id: pickId }, function(e) {
    console.log("获取到材质列表：", e);
  });
}

function firstPerson() {
  let p = {
    location: {
      x: -292.350952,
      y: 627.263977,
      z: 4687.541016
    },
    speed: 100,
    pitch: 0,
    yaw: 0
  };
  ueRender.call(FUNC_CODE.Camera.firstPerson, p);
}

function autoXunjian() {
  let points = [
    {
      boxid: "TriggerBox2",
      isalarm: true,
      labelparam: {
        id: "list111",
        title: "3#水轮发电机组",
        location: { x: 100, y: 100, z: 100 },
        list: [
          { name: "生产状态:", value: "正常" },
          { name: "当前发电量:", value: "9999kwh" },
          { name: "健康状态:", value: "良好" }
        ],
        tipBackgroundColor: "#4FA5F719",
        tipBorderColor: "#FF0000FF",
        tipText:
          "生产厂家名称，用途说明用途说明，规格尺寸型号材aaaa质，颜色，生产日期等设备的基本信息"
      }
    },
    {
      boxid: "TriggerBox3",
      labelparam: {
        id: "list111",
        title: "5#水轮发电机组",
        location: { x: 100, y: 100, z: 100 },
        list: [
          { name: "生产状态:", value: "正常" },
          { name: "当前发电量:", value: "9999kwh" },
          { name: "健康状态:", value: "良好" }
        ],
        tipBackgroundColor: "#4FA5F719",
        tipBorderColor: "#FF0000FF",
        tipText:
          "生产厂家名称，用途说明用途说明，规格尺寸型号材aaaa质，颜色，生产日期等设备的基本信息aaa"
      }
    }
  ];
  let param = {
    lineid: "myspline2",
    duration: 5,
    points: points
  };
  ueRender.custom("Xunjian", param);
}

function thirdPerson() {
  ueRender.call(FUNC_CODE.Camera.thirdPerson, {
    speed: 100,
    pitch: -80,
    location: { x: 0, y: 0, z: 1000 }
  });
}

function objectGet() {
  ueRender.call(FUNC_CODE.Object.get, "Cube2", function(e) {
    console.log("物体坐标：", e);
  });
}

function closeRender() {
  ueRender.close();
}

let bMuted = false;
function muted() {
  ueRender.call(FUNC_CODE.Render.muted, bMuted);
  bMuted = !bMuted;
}
function setsize() {
  ueRender.call(FUNC_CODE.Render.setSize, {
    width: 600,
    height: 500
  });
}

function bitrate() {
  ueRender.call(FUNC_CODE.Render.setBitrateLevel, 1);
}

function addUpdateLabel() {
  let p = {
    type: "mark",
    scale: 1,
    offset: { x: 0, y: 0 },
    clickable: true,
    config: [
      {
        id: "mark1",
        title: "标签#1",
        objectid: "S_ZSMC_24",
        value: "9999",
        valueColor: "#ff0000cc"
      },
      {
        id: "mark2",
        title: "标签#2",
        objectid: "S_JSKZBQ_03",
        value: "2222",
        icon: "mark2"
      },
      {
        id: "mark3",
        title: "标签#3",
        objectid: "S_LWS_08",
        //valueColor:'#000000ee',
        //value:'88888',
        // icon: 'mark3',
        iconUrl: "http://192.168.10.100:3040/static/imgs/person.png"
        // iconUrl: 'http://localhost:8020/static/imgs/person.png'
      }
    ]
  };
  ueRender.call(FUNC_CODE.ScreenLable.AddOrUpdate, p);

  // let p = {
  //                 type: "monitor",
  //                 scale: 1,
  //                 offset: { x: 30, y: 0 },
  //                 config: [
  //                     {
  //                         id: 'monitor1',
  //                         clickable: true,
  //                         objectid: 'S_ZSMC_24',
  //                         title: '2#监测点',
  //                         state: '警告',
  //                         time: '2022-11-11 14:17:39',
  //                         pressure: '99.99',
  //                         temp: '36.85℃'
  //                     }
  //                 ]
  //             };
  // ueRender.call(FUNC_CODE.ScreenLable.AddOrUpdate, p);
}

function addUpdateLabel2() {
  let p = {
    type: "list",
    scale: 1,
    offset: { x: 0, y: 0 },
    clickable: true,
    // backgroundUrl:"http://192.168.40.5:8020/static/",
    config: [
      {
        id: "list1",
        title: "标签#1",
        objectid: "S_ZSMC_24",
        // value: '9999',
        // valueColor: '#ff0000cc',
        list: [
          {
            name: "生产状态",
            value: "正常"
          },
          {
            name: "当前发电量",
            value: "999kwh"
          },
          {
            name: "健康状况",
            value: "良好"
          }
        ]
        // tipText: "新潟县内信息那些信息，新潟县内信息那些信息，新潟县内信息那些信息",
      },
      {
        id: "list2",
        title: "标签#1",
        // objectid: 'S_ZSMC_24',
        location: {
          x: -5355.28662109375,
          y: 682.8953857421875,
          z: 3889.07275390625
        },
        // value: '9999',
        // valueColor: '#ff0000cc',
        list: [
          {
            name: "生产状态",
            value: "正常"
          },
          {
            name: "当前发电量",
            value: "999kwh"
          },
          {
            name: "健康状况",
            value: "良好"
          }
        ]
        // tipText: "新潟县内信息那些信息，新潟县内信息那些信息，新潟县内信息那些信息",
      },
      {
        id: "list3",
        title: "标签#1",
        // objectid: 'S_ZSMC_24',
        location: {
          x: -5760.1201171875,
          y: 683.2874755859375,
          z: 4739.17431640625
        },
        // value: '9999',
        // valueColor: '#ff0000cc',
        list: [
          { name: "岗位：", value: "岗位名称" },
          { name: "部门：", value: "部门名称" },
          { name: "工号：", value: "工号" },
          { name: "位置：", value: "位置信息" }
        ]
        // tipText: "新潟县内信息那些信息，新潟县内信息那些信息，新潟县内信息那些信息",
      }
    ]
  };
  ueRender.call(FUNC_CODE.ScreenLable.AddOrUpdate, p);
}

let labelHidden = false;
function showHideLabel() {
  labelHidden = !labelHidden;
  if (labelHidden) {
    ueRender.call(FUNC_CODE.ScreenLable.hide, "mark*");
  } else {
    ueRender.call(FUNC_CODE.ScreenLable.show, "mark*");
  }
}

function removeLabel() {
  ueRender.call(FUNC_CODE.ScreenLable.remove, "mark*");
}

function waterControll() {
  let param = {
    ids: ["Actor_XSZ_24"],
    runTime: 5,
    zoffset: 10,
    style: {
      volume: 5,
      speedX: 0,
      speedY: 2,
      edge: 10,
      intensity: 1,
      duration: -1
    }
  };
  // //单独调用自定义接口
  // ueRender.custom('WaterControl',param);

  //调用组合接口
  let params = [
    { code: FUNC_CODE.Camera.focus, data: { id: "Actor_XSZ_24" } },
    {
      code: FUNC_CODE.Object.highlight,
      data: { id: "Actor_XSZ_24", isHighlight: true }
    },
    { code: FUNC_CODE.Custom, data: { code: "WaterControl", data: param } },
    {
      code: FUNC_CODE.Object.rotate,
      data: { id: "Actor_XSZ_24", vector: { roll: 10 }, duration: 5 }
    }
  ];
  ueRender.callMulti(params);
}

function combineFunc() {
  let params = [
    {
      code: FUNC_CODE.Object.highlight,
      data: { id: "Cube2", isHighlight: true }
    },
    {
      code: FUNC_CODE.Object.highlight,
      data: { id: "Cube3", isHighlight: true }
    },
    {
      code: FUNC_CODE.Object.rotate,
      data: { id: "Cube2", vector: { yaw: 180 }, duration: 5 }
    },
    {
      code: FUNC_CODE.Object.rotate,
      data: { id: "Cube3", vector: { yaw: 180 }, duration: 5 }
    },
    {
      code: FUNC_CODE.Object.move,
      data: { id: "Cube2", vector: { x: 500 }, duration: 5 }
    },
    {
      code: FUNC_CODE.Object.move,
      data: { id: "Cube3", vector: { x: 500 }, duration: 5 }
    },
    {
      code: FUNC_CODE.Object.scale,
      data: { id: "Cube2", vector: { x: 1 }, duration: 5 }
    },
    {
      code: FUNC_CODE.Object.scale,
      data: { id: "Cube3", vector: { x: 1 }, duration: 5 }
    }
  ];
  ueRender.callMulti(params, function(e) {
    console.log(e);
  });
}

function customFunc() {
  let params = {
    code: "TestLight"
  };
  ueRender.custom("TestLight", params, e => {
    console.log("TestLight", e);
  });
}
